# -*- coding: utf-8 -*-
__author__ = 'fengoupeng'

import sys
sys.path.append('..')
import json

from bottle import route, run, response, request
#import conf
from i_processor.model import JDProduct
from i_util.pymysql import PyMySQL
from i_util import tools
from i_util.i_crawler_services import ThriftScheduler, ThriftDownloader, ThriftExtractor, ThriftProcessor

downloader = ThriftDownloader('127.0.0.1', 7040)
extractor = ThriftExtractor('127.0.0.1', 7060)

# returns:
# {'code': 0, 'errmsg': '', 'result': {}}
def run_test(url, site_id):
	#extractor.reload_parse_config(site_id)
	item_task = {
		'task_id': '112510',
		'doc_type': 'item',
		'download_type': 'phantom',
	}

	try:
		status_body = downloader.download(url, item_task)
		if status_body.status != 'success':
			return {'code': 1, 'errmsg': 'download failed'}
		#print status_body.status
		body = status_body.body
	except Exception as e:
		return {'code': 1, 'errmsg': 'download failed', 'result': repr(e)}

	try:
		item_task['url'] = url
		status_results = extractor.extract(body=body, info=item_task)
		print status_results.status
		return {'results': status_results.results, 'links': status_results.links, 'next_page': status_results.next_page}
	except Exception as e:
		return {'code': 2, 'errmsg': 'extract failed', 'result': repr(e)}

@route('/extractor/test')
def index():
    response.content_type = 'application/json'
    #url = 'http://www.bjztb.gov.cn/zbgg/201507/t9339084.htm'
    #url = 'http://www.bjztb.gov.cn/zbhxrgs_2015/index_1.htm'
    url = request.query.get('url','')
    site_id = request.query.get('site_id', '104')
    callback = request.query.get('callback', '')
    if not url:
        return {'code': 3, 'errmsg': 'illegal parameter'}
    if callback:
        return "%s(%s)" % (callback, json.dumps(run_test(url, site_id)))
    else:
        return json.dumps(run_test(url, site_id))

if __name__ == '__main__':
    #url = 'http://www.bjztb.gov.cn/zbgg/201507/t9339084.htm'
    #site_id = '104'
    #run_test(url, site_id)
    run(host='0.0.0.0', port=8572)
